//
//  UIColorButton.m
//  SmartClothesHanger
//
//  Created by moorgen on 2017/9/8.
//  Copyright © 2017年 MoorgenSmartHome. All rights reserved.
//

#import "UIRadioButton.h"
#import <objc/runtime.h>

@interface UIRadioButton()

@property (nonatomic, copy) ButtonBlock block;

@property (nonatomic, copy) NSString *normalIcon;
@property (nonatomic, copy) NSString *selectedIcon;


@end
@implementation UIRadioButton

- (instancetype)initWithIcon:(NSString *)normalIcon selectedIcon:(NSString *)selectedIcon clickBlock:(ButtonBlock)block {
    self = [super init];
    if (self) {
        [self setNormaIcon:normalIcon selectedIcon:selectedIcon];
        _block = block;
        [self addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}

-(void)setNormaIcon:(NSString *)normalIcon selectedIcon:(NSString *)selectedIcon {
    _normalIcon = normalIcon;
    _selectedIcon = selectedIcon;
    [self setBackgroundImage:[UIImage imageNamed:normalIcon] forState:UIControlStateNormal];
    [self setBackgroundImage:[UIImage imageNamed:selectedIcon] forState:UIControlStateHighlighted];
    [self setBackgroundImage:[UIImage imageNamed:selectedIcon] forState:UIControlStateSelected];
    [self setBackgroundImage:[UIImage imageNamed:selectedIcon] forState:UIControlStateSelected | UIControlStateHighlighted];
}


- (void)action:(id)sender
{
    self.selected = !self.selected;
    if (self.block)
    {
        self.block(self);
    }
}

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)controlEvents {
    if (controlEvents == UIControlEventTouchUpInside) {
        target = self;
        action = @selector(action:);
    }
    [super addTarget:target action:action forControlEvents:controlEvents];
}

@end

